package com.bjsxt.shop.goods.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.common.util.PageInfoUtil;
import com.bjsxt.shop.common.util.RedisUtil;
import com.bjsxt.shop.goods.dao.IAGoodsBrandDao;
import com.bjsxt.shop.goods.dao.IAGoodsCateDao;
import com.bjsxt.shop.goods.dao.IAGoodsDao;
import com.bjsxt.shop.goods.dao.IAUsersCarDao;
import com.bjsxt.shop.goods.pojo.AGoods;
import com.bjsxt.shop.goods.pojo.AGoodsBrand;
import com.bjsxt.shop.goods.pojo.AGoodsCate;
import com.bjsxt.shop.goods.pojo.AUsersCar;
import com.bjsxt.shop.goods.service.IGoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("goodsService")
public class GoodsServiceImpl implements IGoodsService
{
	@Resource
	private IAGoodsBrandDao goodsBrandDao;
	@Resource
	private IAGoodsCateDao goodsCateDao;
	@Resource
	private IAGoodsDao goodsDao;
	
	@Resource
	private IAUsersCarDao usersCarDao;
	
	@Resource
	private RedisUtil redisUtil;
	/**
	 * 品牌操作开始	 */
	@Override
	public AGoodsBrand findOneGoodsBrandService(Map<String, Object> condMap)
	{
		return this.goodsBrandDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneGoodsBrandService(AGoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsBrandDao.save(goodsBrand);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsBrand.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject saveOneGoodsBrandService(AGoodsBrand goodsBrand,Map<String, Object> paramsMap)
	{
		/* 保存品牌 */
		JSONObject resultJSON = this.saveOneGoodsBrandService(goodsBrand);
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneGoodsBrandService(AGoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsBrandDao.update(goodsBrand);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsBrand.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneGoodsBrandService(AGoodsBrand goodsBrand,Map<String, Object> paramsMap)
	{
		JSONObject resultJSON = this.updateOneGoodsBrandService(goodsBrand);
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneGoodsBrandService(AGoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		return resultJSON;
	}
	
	@Override
	public Map<String,Object> findCondListGoodsBrandService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		/* 为关键字加上%,实现模糊匹配 ，模糊查询的要写在serviceimpl层*/
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			/* 分页 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			/* 查询总记录数 */
			List<AGoodsBrand> roleList = this.goodsBrandDao.findList(condMap);
			/* 使用Dubbo的时候,不能为参数新增加赋值 
			 * 设置TotalRecord的时候一定要用page。getTotal*/
			/*Long.valueOf(page.getTotal()).intValue()转化类型
			 *注意利用*/
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
			resultMap.put("list", roleList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap;
		}
		/* 不分页查询数据 */
		List<AGoodsBrand> roleList= this.goodsBrandDao.findList(condMap);
		resultMap.put("list", roleList);
		return resultMap ; 
	}
	/**
	 * 品牌操作结束
	 */
	
	
	/**
	 *商品分类操作开始 
	 */
	@Override
	public AGoodsCate findOneGoodsCateService(Map<String, Object> condMap)
	{
		return this.goodsCateDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneGoodsCateService(AGoodsCate goodsCate)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsCateDao.save(goodsCate);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsCate.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneGoodsCateService(AGoodsCate goodsCate)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsCateDao.update(goodsCate);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsCate.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneGoodsCateService(AGoodsCate goodsCate)
	{
		JSONObject resultJSON = new JSONObject();
		return resultJSON;
	}
	
	@Override
	public Map<String, Object> findCondListGoodsCateService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		Map<String,Object> resultMap=new HashMap<String,Object>();
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AGoodsCate> roleList = this.goodsCateDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			resultMap.put("list", roleList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap;
		}
		/* 不分页查询数据 */
		List<AGoodsCate> roleList = this.goodsCateDao.findList(condMap);
		resultMap.put("list", roleList);
		return resultMap;
	}
	
	/**
	 *商品分类操作结束
	 */
	
	/**
	 *商品操作开始 
	 */
	@Override
	public AGoods findOneGoodsService(Map<String, Object> condMap)
	{
		return this.goodsDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneGoodsService(AGoods goods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsDao.save(goods);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	/**
	 * 保存一条商品
	 * @param goods	保存的对象
	 * @return	json:{"code":"0","info":"成功","data":{"effect":"","id":""}}
	 */
	public JSONObject saveOneGoodsService(AGoods goods,Map<String, Object> paramsMap)
	{
		JSONObject resultJSON = this.saveOneGoodsService(goods);
		return resultJSON ; 
	}
	
	@Override
	public JSONObject updateOneGoodsService(AGoods goods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsDao.update(goods);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneGoodsService(AGoods goods)
	{
		JSONObject resultJSON = new JSONObject();
		return resultJSON;
	}
	
	@Override
	public Map<String,Object> findCondListGoodsService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		Map<String,Object> resultMap=new HashMap<String,Object>();
		/* 为关键字加上%,实现模糊匹配 
		 * condMap用来传递参数，参数从jsp页面接收*/
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			/* 分页 */
			Page page=PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			List<AGoods> goodsList=this.goodsDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
			resultMap.put("list", goodsList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap;
		}
		/* 不分页查询数据 
		 * 集合转化为list可以利用collection.emptyList（）
		 * 这样所有的返回的list都可以用一个list来表示，节省内存*/
		List<AGoods> goodsList=this.goodsDao.findList(condMap);
		resultMap.put("list", goodsList);
		return resultMap;
	}

	/*利用service的方法形成一个树
	 * 为了出树，需要利用递归（需要注意停止的条件、传递参数设置值、遍历不同的参数运行停止的条件）*/
	@Override
	public List<AGoodsCate> findTreeListCateService()
	{
		/*设置一个结果集合，判断集合的种类
		 * 传递参数来接收递归中传递过来的值		 */
		List<AGoodsCate> resultList=new ArrayList<AGoodsCate>();
		/* 先查询出所有的一级节点 */
		Map<String,Object> condMap=new HashMap<String, Object>();
		condMap.put("parentId", "0");
		/*首先借用service中的方法	 */
		Map<String, Object> resultMap = this.findCondListGoodsCateService(null, condMap);
		List<AGoodsCate> cateList=(List<AGoodsCate>) resultMap.get("list");
		for (Iterator iterator = cateList.iterator(); iterator.hasNext();)
		{
			AGoodsCate cateTemp = (AGoodsCate) iterator.next();
			this.findTreeListCateServiceUtil(cateTemp,0,resultList);
		}
		return resultList;
	}
	
	/*递归出树方法
	 * 存储结果:参数来存储结果	 */
	public void findTreeListCateServiceUtil(AGoodsCate cate,int level,List<AGoodsCate> goodsCateList)
	{
		StringBuffer preStrsb=new StringBuffer();
		for (int i = 0; i < level; i++)
		{
			preStrsb.append("|-->");
		}
		/*设置树形结构名字，通过在pojo中另外设置一个TreeName的属性进行设置
		 * 同时不改变cate里面的name属性		 */
		cate.setTreeName(preStrsb.toString()+cate.getName());
		/*System.out.println("GoodsServiceImpl.findTreeListCateServiceUtil()"+cate.getTreeName());*/
		/*获取传递进来的值，把该值储存在goodsCateList中
		 * 这里先储存一下cate的值		 */
		goodsCateList.add(cate);
		/*开始查询子节点
		 * 查询要一条一条的查询，不要借助于dubbo去查询树，用到什么就去查询什么		 */
		Map<String,Object> condMap=new HashMap<String,Object>();	
		condMap.put("parentId", cate.getId());
		/*当需要子节点的时候，利用dubbo会自己调用自己而不能赋值成功，所以一般需要再次查询一遍即可
		 * 注意此时要注释掉另外发送一条sql语句中的注释
		 * 之所以可以查到数据是因为有parentId，在mapper文件中也有一个通过parentId查询的语句		 */
		Map<String,Object> resultMap=this.findCondListGoodsCateService(null,condMap);
		List childList=(List) resultMap.get("list");
		if(childList.size() > 0 )
		{
			/* 非叶子节点 
			 * 用来设置非叶子节点的属性，在jsp中显示的时候做判断*/
			cate.setLeafStatus("0");
			for (Iterator iterator = childList.iterator(); iterator.hasNext();)
			{
				AGoodsCate cateTmpe = (AGoodsCate) iterator.next();
				/* 自己调用自己,参数会发生变化, */
				this.findTreeListCateServiceUtil(cateTmpe,level + 1,goodsCateList);
			}
		}else
		{
			/* 叶子节点 */
			cate.setLeafStatus("1");
		}
	}
	/**
	 *商品分类操作结束
	 */
	
	/**
	 * 购物车操作开始
	 */
	
	@Override
	public AUsersCar findOneUsersCarService(Map<String, Object> condMap)
	{
		return this.usersCarDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneUsersCarService(AUsersCar usersCar)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.usersCarDao.save(usersCar);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", usersCar.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneUsersCarService(AUsersCar usersCar)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.usersCarDao.update(usersCar);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", usersCar.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneUsersCarService(AUsersCar usersCar)
	{
		return null;
	}
	
	@Override
	public Map<String, Object> findCondListUsersCarService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		Map<String,Object> resultMap = new HashMap<String, Object>();
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage() ,pageInfoUtil.getPageSize());
			List<AUsersCar> roleList = this.usersCarDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			
			resultMap.put("list", roleList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap ; 
		}
		/* 不分页查询数据 */
		List<AUsersCar> roleList = this.usersCarDao.findList(condMap);
		resultMap.put("list", roleList);
		return resultMap ; 
	}
	
	/**
	 * 购物车操作结束
	 */
}
